var util = require('../../utils/MD5.js');
var myjson = require('../../utils/JSON.js');
var intervarID
Page({
  data: {
    id:"",
    startTimes: "",
    allRuningTime: "",
    runningTimes: "",
    dayRuningTime: "",
    bumpConsume: "",
    bumpElectricity: "",
    animation:{},
    bugRun:"",
    bugStop: "",
    bugSoHot: "",
    bugInWater: "",
    bugElectricLeakage: "",
    bugPhaseLoss: "",
    bugOverLoad: "",
    bugBlocking:"",
    bugClockwise:"",
    bugAnticlockwise:""
  },
  onReady:function(){
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 1000, 
      timingFunction: 'linear',
      // 延迟多长时间开始
      delay: 100,
    });
  },
  onLoad: function () {

    wx.showLoading({
      title: '数据获取中....',
    })

    var that = this
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;  
    
    var sid = wx.getStorageSync('sid');
    var token = timestamp + "@13f@" + sid;  
    var id = wx.getStorageSync('id'); 
    token = util.hexMD5(token); 

    console.log("sid:" + sid+" token:"+token+" timestamp:"+timestamp);
    wx.request({
      url:"https://www.gzcri.cn/api/bump/item/query.json",
      data:{
        code: timestamp,
        token:token,
        sid: sid,
        index:id
      },
      header: { "content-type":"application/x-www-form-urlencoded"},
      method:"POST",
      fail:function(res){
        wx.hideLoading();
      },
      success:function(res){
        wx.hideLoading();
        var responseData = res.data;
        console.log(responseData.result);
        if(res.data.status == -1){
        }else{
          // wx.setStorageSync('bumpdetail', responseData.data)
         
          var fixstartTimes = responseData.data.startTimes * 1;
          var fixallRuningTime = responseData.data.allRuningTime * 1;
          var fixruningTimes = responseData.data.runningTimes * 1;
          var fixdayRuningTime = responseData.data.dayRuningTime * 1;
          var fixbumpConsume = responseData.data.bumpConsume * 1;
          var fixbumpElectricity = responseData.data.bumpElectricity * 1;

          var fixbugRun = responseData.data.bugRun * 1;
          var fixbugStop = responseData.data.bugStop * 1;
          var fixbugSoHot = responseData.data.bugSoHot * 1;
          var fixbugInWater = responseData.data.bugInWater * 1;
          var fixbugElectricLeakage = responseData.data.bugElectricLeakage * 1;
          var fixbugPhaseLoss = responseData.data.bugPhaseLoss * 1;
          var fixbugOverLoad = responseData.data.bugOverLoad * 1;
          var fixbugAnticlockwise= responseData.data.bugAnticlockwise * 1;
          var fixbugBlocking = responseData.data.bugBlocking * 1;
          var fixbugClockwise = responseData.data.bugClockwise * 1;
          console.log("success!"+fixbugRun);
          that.setData({
            'id':id,
            'startTimes': fixstartTimes,
            'allRuningTime': fixallRuningTime,
            'runningTimes': fixruningTimes,
            'dayRuningTime': fixdayRuningTime,
            'bumpConsume': fixbumpConsume.toFixed(2),
            'bumpElectricity': fixbumpElectricity.toFixed(2),
            'bugRun': fixbugRun,
            'bugStop': fixbugStop,
            'bugSoHot': fixbugSoHot,
            'bugInWater': fixbugInWater,
            'bugElectricLeakage': fixbugElectricLeakage,
            'bugPhaseLoss': fixbugPhaseLoss,
            'bugOverLoad': fixbugOverLoad,
            'bugAnticlockwise':fixbugAnticlockwise,
            'bugBlocking': fixbugBlocking,
            'bugClockwise': fixbugClockwise
          });
          
          this.animation = wx.createAnimation({
            duration: 1000,
          })

          intervarID = setInterval(function () {
            console.log("开始说话.....")
            this.animation.scale(2, 2).step();//修改透明度,放大  
            this.animation.scale(1, 1).step();
            that.setData({
              animation: this.animation.export()
            });
          }.bind(this), 2000);
        }
      }
    })
  },
  clearData: function (e) {
    //获得表单数据
    var that = this

    let map = new Map();
    var $id = e.currentTarget.id;
    if ($id == 100) {
      map.set('runningTimesClear', '1');
    } else if ($id == 101) {
      map.set('allRuningTimeClear', '1');
    } else if ($id == 102) {
      map.set('startTimesClear', '1');
    } else if ($id == 103) {
      map.set('dayRuningTimeClear', '1');
    } else if ($id == 104) {
      map.set('bumpConsumeClear', '1');
    } else if ($id == 105) {
      map.set('bumpElectricityClear', '1');
    }

    var json = myjson.mapToJson(map);
    console.log('json:' + json +" id:"+$id);
    var sid = wx.getStorageSync('sid');
    var timestamp = Date.parse(new Date());

    timestamp = timestamp / 1000;
    var token = timestamp + "@13f@" + sid
    token = util.hexMD5(token);
    var id = wx.getStorageSync('id'); 

    wx.request({
      url: "https://www.gzcri.cn/api/bump/edit.d",
      data: {
        code: timestamp,
        token: token,
        data: json,
        sid: sid,
        index:id
      },
      header: { "content-type": "application/x-www-form-urlencoded" },
      method: "POST",
      success: function (res) {
        var responseData = res.data;
        console.log(responseData.result);
        if (res.data.status == -1) {
          console.log(responseData.result);
        } else {

          if ($id == 100) {
            that.setData({
              'runningTimes': 0,
            });
          } else if ($id == 101) {
            that.setData({
              'allRuningTime': 0,
            });
          } else if ($id == 102) {
            that.setData({
              'startTimes': 0,
            });
          } else if ($id == 103) {
            that.setData({
              'dayRuningTime': 0,
            });
          } else if ($id == 104) {
            that.setData({
              'bumpConsume': 0,
            });
          } else if ($id == 105) {
            that.setData({
              'bumpElectricity': 0,
            });
          }
          console.log('success');
        }
      }
    })
  },

  speaking:function () {
    console.log("动画.....")
    var _this = this;
    

    var animation = wx.createAnimation({
      duration: 1000,
    })
  animation.opacity(0).scale(3, 3).step();//修改透明度,放大  
    this.setData({
      spreakingAnimation: animation.export()
    })
  },

  onUnload: function () {
    clearInterval(intervarID);
  },
})
